package com.cos.hospital.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import com.cos.hospital.R;
import com.cos.hospital.code.QueryType;
import com.cos.hospital.code.helper.KTextWatcher;
import com.cos.hospital.init.BaseFragment;
import com.cos.hospital.model.bean.PatientListBean;
import com.cos.hospital.model.event.DoSearchEvent;
import com.cos.hospital.model.event.PatientInfoEditEvent;
import com.cos.hospital.model.event.ReceptionPersonNumEvent;
import com.cos.hospital.model.event.SearchContentEvent;
import com.cos.hospital.tool.USnackbar;
import com.cos.hospital.ui.main.adapter.FragmentAdapter;
import com.cos.hospital.widget.TopTabView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 */
public class Tab1Fragment extends MainFragment {
    @BindView(R.id.edt_search)
    EditText edt_search;
    @BindView(R.id.top_tab)
    TopTabView topTabView;
    @BindView(R.id.content)
    ViewPager viewPager;
    FragmentAdapter adapter;
    MainFragment[] fragment = new MainFragment[2];

    public Tab1Fragment() {
        // Required empty public constructor
    }


    public static Tab1Fragment newInstance() {
        Tab1Fragment fragment = new Tab1Fragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    protected int layout() {
        return R.layout.fragment_tab1;
    }

    @Override
    protected void init() {
        topTabView.setContent("未接诊", "已接诊");
        topTabView.setPerson1(0);
        topTabView.setPerson2(0);
        if (fragment[0] == null)
            fragment[0] = new ReceptionFragment(QueryType.UN_RECEPTION);
        if (fragment[1] == null)
            fragment[1] = new ReceptionFragment(QueryType.HAVED_RECEPTION);
        //设置布局管理器
        LinearLayoutManager topLayoutManager = new LinearLayoutManager(getActivity());
        topLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        adapter = new FragmentAdapter(getChildFragmentManager(), fragment);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                topTabView.setSelect(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        topTabView.setOnChangeClick(new TopTabView.OnChangeClick() {
            @Override
            public void onChange(int positon) {
                viewPager.setCurrentItem(positon);
            }
        });
    }

    @Override
    protected void logic() {
        edt_search.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String content = textView.getText() + "";
                if (content.length() != 0) {
                    DoSearchEvent event = new DoSearchEvent();
                    event.queryType = QueryType.UN_RECEPTION;
                    event.content = content;
                    EventBus.getDefault().post(event);
                } else {
                    USnackbar.showError(context, "请输入关键字");
                }
            }
            return false;
        });
        edt_search.addTextChangedListener(new KTextWatcher() {
            @Override
            public void afterTextChanged(Editable txtEnd) {
                SearchContentEvent SearchContentEvent = new SearchContentEvent();
                SearchContentEvent.queryType = QueryType.UN_RECEPTION;
                SearchContentEvent.txtEnd = txtEnd;
                EventBus.getDefault().post(SearchContentEvent);
            }
        });
    }

    @Override
    protected void refresh() {
        for (MainFragment mainFragment : fragment) {
            if (mainFragment != null)
                mainFragment.refresh();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateInfo(ReceptionPersonNumEvent event) {
        if (event != null) {
            System.out.println("数据==》》" + event.queryType + "===" + event.perosnNum);
            if (event.queryType == QueryType.UN_RECEPTION) {
                topTabView.setPerson1(event.perosnNum);
            } else if (event.queryType == QueryType.HAVED_RECEPTION) {
                topTabView.setPerson2(event.perosnNum);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @OnClick(R.id.iv_scan)
    public void scan() {
        startActivity(new Intent(context, QRCodeActivity.class));
    }

}
